<?php
function absen_upload_kalkulasi(){
	$periode_id = get_periode_absen();
	$dd = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = date('m',strtotime($dd['tanggal']));
	$tahun = date('Y',strtotime($dd['tanggal']));
	$end = $tahun.'-'.$bulan.'-11';  //hasil list kalender 10

	if($bulan == '01')
		$start = ($tahun-1).'-12-11';  	
	else
		$start = $tahun.'-'.sprintf('%02d', (int)$bulan-1).'-11'; 

	$query = "SELECT * FROM karyawan 
		WHERE karyawan_id 
		NOT IN (SELECT karyawan_id FROM karyawan_disable)
		AND karyawan_id NOT IN (SELECT karyawan_id FROM pemberhentian WHERE TMT < '{$end}')
		ORDER BY karyawan_id ASC";
	$res = my_query($query);
	while($ey = my_fetch_array($res)){
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		if($karyawan['finger_diabaikan'] =='Y'){
			check_update_diabaikan($ey['karyawan_id'],
				$start,
				$end,
				$karyawan['waktu_kerja_id'],
				$karyawan['kelompok_shift_id'],
				$karyawan['kelompok']); 
		}else{
			check_update_flexi_or_shif($ey['karyawan_id'],
				$start,
				$end,
				$karyawan['waktu_kerja_id'],
				$karyawan['kelompok_shift_id'],
				$karyawan['kelompok'],
				$karyawan['nik']); 
		}
	}
	return true;
}

function check_update_diabaikan($karyawan_id, $start ,$end, $waktu_kerja_id, $kelompok_shift_id, $kelompok_by_karyawan){
	$tgls = list_kalender($start , $end);
	$periode_id = get_periode_absen(); 
	foreach($tgls as $tgl){ 
		if($waktu_kerja_id > 3){  //shift
			$kelompok = get_kelompok_shift_jadwal($tgl,$periode_id ,$kelompok_shift_id);
			if($kelompok=='X'){
				$status_absen_id=2 ; //X
			}else{
				$status_absen_id=(int) $kelompok+2 ; //1,2,3
			}
		}else{ //non shift
			if(get_hari_libur($tgl)){	
				$kelompok= 'X';
				$status_absen_id=2 ; //X
			}else{
				$kelompok = $kelompok_by_karyawan;
				$status_absen_id=1 ; //N
			}	
		}
		stor_realisasi_non_finger( $karyawan_id,$tgl ,  $kelompok, $waktu_kerja_id, $status_absen_id );
	} 
}

function stor_realisasi_non_finger( $karyawan_id, $date,  $kelompok, $waktu_kerja_id, $status_absen_id ){

	$tgl=date('d', strtotime($date));
	$query = "SELECT * FROM a_realisasi 
		WHERE karyawan_id = {$karyawan_id}
		AND field_tgl='tgl_{$tgl}' ";
	$res = my_query($query);
	if(my_num_rows($res) > 0){
		$reals = my_fetch_array($res);
		$realisasi_id=$reals['realisasi_id'];
		$sc_jadwal_kerja_kelompok_id=(int) $reals['sc_jadwal_kerja_kelompok_id'];
	}else{
		$realisasi_id=$sc_jadwal_kerja_kelompok_id=0 ;
	}
	if($sc_jadwal_kerja_kelompok_id==0){
		$query = "SELECT * FROM a_jadwal_kerja_kelompok 
			WHERE 	waktu_kerja_id = {$waktu_kerja_id}
			AND kelompok='{$kelompok}' ";
		$res = my_query($query);
		$reals = my_fetch_array($res);
		$sc_jadwal_kerja_kelompok_id=(int) $reals['jadwal_kerja_kelompok_id'];
	}	
	
	if($kelompok =='X'){
		$status=2 ;//X
		$datas = array(
			'karyawan_id'=>my_type_data_int($karyawan_id), 	
			'field_tgl' =>my_type_data_str('tgl_'. date('d', strtotime($date)) ),	
			'jam_datang' =>my_type_data_str('00:00:00'),	
			'jam_pulang'=>my_type_data_str('00:00:00'),
			'status_absen_id'=>my_type_data_int($status), 	
			'sc_jadwal_kerja_kelompok_id'=>my_type_data_int($sc_jadwal_kerja_kelompok_id), 	
		); 	
	}else{
		$status=get_status_absen_id( $karyawan_id, $date, $status_absen_id);
		if($status<=5){ //N,1,2,3
			$hari_id = get_hari_id_by_date($date);
			$query = "SELECT jam_masuk,jam_pulang FROM a_jadwal_kerja 
			WHERE waktu_kerja_id = {$waktu_kerja_id}
			AND kelompok ='{$kelompok}' 
			AND hari_id ={$hari_id} ";
			$res = my_query($query);
			$row = my_fetch_array($res);
			$datas = array(
				'karyawan_id'=>my_type_data_int($karyawan_id), 	
				'field_tgl' =>my_type_data_str('tgl_'. date('d', strtotime($date)) ),	
				'jam_datang' =>my_type_data_str($row['jam_masuk']),	
				'jam_pulang'=>my_type_data_str($row['jam_pulang']),
				'status_absen_id'=>my_type_data_int($status), 	
				'sc_jadwal_kerja_kelompok_id'=>my_type_data_int($sc_jadwal_kerja_kelompok_id), 	
			); 	
		}else{
			$datas = array(
				'karyawan_id'=>my_type_data_int($karyawan_id), 	
				'field_tgl' =>my_type_data_str('tgl_'. date('d', strtotime($date)) ),	
				'jam_datang' =>my_type_data_str('00:00:00'),	
				'jam_pulang'=>my_type_data_str('00:00:00'),
				'status_absen_id'=>my_type_data_int($status), 	
				'sc_jadwal_kerja_kelompok_id'=>my_type_data_int($sc_jadwal_kerja_kelompok_id), 	
			); 	
		}	
	}
	if($realisasi_id >0 ){
		my_update_record('a_realisasi', 'realisasi_id' , $realisasi_id , $datas);
		return $realisasi_id;
	}
	return my_insert_record('a_realisasi',$datas);
}

function check_update_flexi_or_shif($karyawan_id, $start ,$end, $waktu_kerja_id, $kelompok_shift_id, $kelompok, $nik){

	$periode_id = get_periode_absen();
	$tgls = list_kalender($start , $end);
	$kelompok1=$kelompok; //kelompok1 nanti disinkronisasi
	 
	foreach($tgls as $tgl){ 
		
		if($waktu_kerja_id > 3){ //shift
			$kelompok = get_kelompok_shift_jadwal($tgl,$periode_id ,$kelompok_shift_id);
			$kelompok1=$kelompok;
			if($kelompok=='X'){
				$libur=true;
			}else{
				$libur=false;
			}
		}else{  //non shift
			if(get_hari_libur($tgl)){	
				$libur=true;
				$kelompok ='X';
				$kelompok1=$kelompok;
			}else{
				$libur=false;
				$kelompok1=$kelompok;
				$kelompok ='N';
			}	
		}	
		stor_realisasi( $karyawan_id, $nik , $tgl, $libur, $kelompok, $waktu_kerja_id, $kelompok1 );
	} 

	}


function stor_realisasi($karyawan_id,$nik , $date, $libur, $kelompok, $waktu_kerja_id, $kelompok1){

	$tgl=date('d', strtotime($date));
	$query = "SELECT * FROM a_realisasi 
		WHERE karyawan_id = {$karyawan_id}
		AND field_tgl='tgl_{$tgl}' ";
	$res = my_query($query);
	if(my_num_rows($res) > 0){
		$reals = my_fetch_array($res);
		$realisasi_id=$reals['realisasi_id'];
		$sc_jadwal_kerja_kelompok_id=(int) $reals['sc_jadwal_kerja_kelompok_id'];
	}else{
		$realisasi_id=$sc_jadwal_kerja_kelompok_id=0 ;
	}
	if($sc_jadwal_kerja_kelompok_id==0){	
		$query = "SELECT * FROM a_jadwal_kerja_kelompok 
			WHERE 	waktu_kerja_id = {$waktu_kerja_id}
			AND kelompok='{$kelompok1}' ";
		$res = my_query($query);
		$reals = my_fetch_array($res);
		$sc_jadwal_kerja_kelompok_id=(int) $reals['jadwal_kerja_kelompok_id'];
	}	
	$status=get_status_absen_id( $karyawan_id, $date,0);
	$check_file_upload = get_absen_upload($nik,$date); 
	
	if($check_file_upload['datang']=='00:00:00'){
		$jam_datang='';
	}else{	
		$jam_datang=$check_file_upload['datang'] ;
	}
	if($check_file_upload['pulang']=='00:00:00'){
		$jam_pulang='';
	}else{	
		$jam_pulang=$check_file_upload['pulang'] ;
	}
	
	if($status==0){
		if($jam_datang=='' AND $jam_pulang==''){
			if($libur){
				$status=2 ; //X
			}else{
				$status=16 ; //A
			}
		}else{
			if($kelompok=="1" OR 
				$kelompok=="2" OR
				$kelompok=="3"){
					$status=(int) $kelompok+2 ; //1,2,3	
			}else{
				$status=1 ; //N
			}
		}
	}	
	$datas = array(
			'karyawan_id'=>my_type_data_int($karyawan_id), 	
			'field_tgl' =>my_type_data_str('tgl_'. date('d', strtotime($date)) ),	
			'jam_datang' =>my_type_data_str($jam_datang),	
			'jam_pulang'=>my_type_data_str($jam_pulang),
			'status_absen_id'=>my_type_data_int($status), 
			'sc_jadwal_kerja_kelompok_id'=>my_type_data_int($sc_jadwal_kerja_kelompok_id), 	
	);
	if($realisasi_id >0 ){
		my_update_record('a_realisasi', 'realisasi_id' , $realisasi_id , $datas);
		return $realisasi_id;
	}
	return my_insert_record('a_realisasi',$datas);
}

function page_kalkulasi(){
	my_set_code_js('
	$(document).ready(function() {
       $(\'#onebar\').load(\'index.php?com='.$_GET['com'].'&task=calc\'); 
	 
    });
	');
	return '<div id="onebar" style="width:100%;text-align:center;"><img src="components/system/jquery/facebox/loading.gif" /><br/>On processing!!!</div>';
}

function check_data_exists($karyawan_id , $date){
	$tgl=date('d', strtotime($date));
	$query = "SELECT * FROM a_realisasi 
		WHERE karyawan_id = {$karyawan_id}
		AND field_tgl='tgl_{$tgl}' ";
	$res = my_query($query);
	if(my_num_rows($res) > 0){
		$row = my_fetch_array($res);
		return $row['realisasi_id'];
	}	
	return false;
}

function get_kelompok_shift_jadwal($tanggal , $periode_id , $kelompok_shift_id ){
	list($yyyy,$mm,$dd) = explode("-" , $tanggal);
	$query = "SELECT tgl_{$dd} FROM a_kalendar_shift 
		WHERE periode_id = {$periode_id} 
		AND kelompok_shift_id={$kelompok_shift_id}";
	$result = my_query($query);
	$re = my_fetch_array($result);
	$datas = array();
	return $re['tgl_'.$dd];  
}

function get_hari_id_by_date($date){
	$hari_id=date("N",strtotime($date) ); //Monday to Sunday
	//konversi ke Sunday to Saturday
	if($hari_id==7){
		$hari_id=1;
	}else{
		$hari_id++;
	}		
	return $hari_id;
} 

function  get_absen_upload($nik,$date){
	//$query = "SELECT * FROM a_upload_capture WHERE nik='{$nik}' AND 	tanggal ='{$date}'";
	$res = array();
	$res['datang'] = get_smallesttime($nik,$date);
	$res['pulang'] = get_bigesttime($nik,$date);
	return  $res ;
}

function get_smallesttime($nik,$tanggal){
	$query = "SELECT datang FROM a_upload_capture WHERE nik ='{$nik}' AND tanggal ='{$tanggal}' ORDER BY datang ASC LIMIT 1";
	$result = my_query($query);
	$row = my_fetch_array($result);
	return $row['datang'];
}

function get_bigesttime($nik,$tanggal){
	$query = "SELECT pulang FROM a_upload_capture WHERE nik ='{$nik}' AND tanggal ='{$tanggal}' ORDER BY pulang DESC LIMIT 1";
	$result = my_query($query);
	$row = my_fetch_array($result);
	return $row['pulang'];
}

function get_hari_libur($date){
	if(is_sabtu_minggu($date)){
		return true;
	}
	$query="SELECT hari_libur_id FROM a_hari_libur
		WHERE tanggal = '{$date}'";
	if (my_num_rows( my_query($query)) >0){	
		return true;
	}	
	return false;
}

function get_status_absen_id($karyawan_id , $date, $status_absen_id   ){

	//cuti
	$query="SELECT kelompok_cuti_id FROM surat_cuti 
		WHERE tanggal_mulai <='{$date}' 
		AND tanggal_selesai >='{$date}' 
		AND karyawan_id ={$karyawan_id}";	
	$result = my_query($query);
	if( my_num_rows($result) > 0 ){
		$statuss=my_fetch_array(my_query($query));	
		return $statuss['kelompok_cuti_id'];
	}
	
	//surat ijin
	$query="SELECT status_absen_id FROM a_surat_ijin 
		WHERE full_day='Y' 
		AND tanggal ='{$date}' 
		AND karyawan_id ={$karyawan_id}";	
	$result = my_query($query);
	if( my_num_rows($result) > 0 ){
		$statuss=my_fetch_array(my_query($query));	
		return $statuss['status_absen_id'];
	}	

	//spd
	$query="SELECT * FROM spd
		WHERE '".$date."' BETWEEN tanggal_mulai AND tanggal_selesai 
		AND tipe_karyawan='Karyawan' 
		AND karyawan_id ={$karyawan_id}
	";
	$result = my_query($query);
	if( my_num_rows($result) > 0 ){
		return 15 ; //sementara hardcore dulu
	}	
	return $status_absen_id;
}

